package com.zjdiepu.www.manage.view;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.zjdiepu.www.base.BaseController;
import com.zjdiepu.www.base.enums.type.DataDicType;
import com.zjdiepu.www.base.enums.type.ResultFlagType;
import com.zjdiepu.www.commpent.ViewScopeControllerHelper;
import com.zjdiepu.www.ibiz.cache.ICachedDictionaryService;
import com.zjdiepu.www.ibiz.dh.IDhInsuranceCompulsoryService;
import com.zjdiepu.www.ibiz.dh.IDhInsuranceRenewalService;
import com.zjdiepu.www.ibiz.dh.IDhInsuranceService;
import com.zjdiepu.www.model.base.dto.SysUser;
import com.zjdiepu.www.model.dh.dto.DhInsurance;
import com.zjdiepu.www.model.dh.dto.DhInsuranceCompulsory;
import com.zjdiepu.www.model.dh.ext.DhInsuranceCompulsoryExt;
import com.zjdiepu.www.model.dh.ext.DhInsuranceExt;
import com.zjdiepu.www.model.dh.ext.DhInsuranceRenewalExt;
import com.zjdiepu.www.model.yw.dto.YwBusinessCar;
import com.zjdiepu.www.model.yw.response.YwBusinessDetail;
import com.zjdiepu.www.util.ExceptionHelper;

/**
 * 续保预警Controller层<br/>
 * @author xiongxiaotun<br/>
 * @time 2017年05月11日 20时36分<br/>
 * @since 1.0<br/>
 */
@Controller
@RequestMapping(value= "DhInsuranceRenewalViewController", produces="text/html;charset=UTF-8")
@Api(tags = {"续保预警"}, produces="text/html;charset=UTF-8")
public class DhInsuranceRenewalViewController extends BaseController {
    @Resource
    private IDhInsuranceRenewalService dhInsuranceRenewalService;
    @Resource
    private IDhInsuranceService dhInsuranceService;
    @Resource
    private IDhInsuranceCompulsoryService dhInsuranceCompulsoryService;
    @Resource
    private ViewScopeControllerHelper viewScopeControllerHelper;
    @Resource
    private ICachedDictionaryService cachedDictionaryService;
    /**
     * @author xiongxiaotun <br/>
     * @Title: insuranceRenewalDetail  <br/>
     * @Description: 续保详情 <br/>
     * @param model
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "续保详情", response = Boolean.class, httpMethod="GET", tags={"续保"})
    @RequestMapping(value = "insuranceRenewalDetail", method = RequestMethod.GET)
    public String insuranceRenewalDetail(Model model, String id, String insuranceRenewalId) {
    	if (StringUtils.isBlank(id)) {
			ExceptionHelper.thow(ResultFlagType.PARAM_EMPTY);
		}
    	//下拉选项
    	model.addAttribute("insurantType", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.INSURANT_TYPE,"请选择..."));
    	model.addAttribute("insurantYear", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.INSURANT_YEAR,"请选择..."));
    	model.addAttribute("insurant", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.INSURANT,"请选择..."));
    	model.addAttribute("insuranceEmployee", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.INSURANCE_EMPLOYEE,"请选择..."));
    	model.addAttribute("insuranceCompany", cachedDictionaryService.getValueNamesWithSpaceOption(DataDicType.INSURANCE_COMPANY,"请选择..."));
    	
    	
    	viewScopeControllerHelper.preparedNowTime(model);
    	YwBusinessDetail ywBusinessDetail = viewScopeControllerHelper.preparedBusiness(id, model);
    	YwBusinessCar car = viewScopeControllerHelper.preparedCar(id,ywBusinessDetail.getCustomerId(), model);
    	if(car != null) {
    		viewScopeControllerHelper.preparedLoan(id, car.getId(), model, car.getCarType());
    	}
    	if(ywBusinessDetail != null) {
    		viewScopeControllerHelper.preparedHistoryBusiness(ywBusinessDetail.getCustomerNo(), model);
    		viewScopeControllerHelper.preparedRegistrationPlates(id, model);
    		if(StringUtils.isNotBlank(insuranceRenewalId)){
//    			DhInsuranceRenewal dhInsuranceRenewal = dhInsuranceRenewalService.selectById(insuranceRenewalId);
//    			if(dhInsuranceRenewal != null) {
//    				model.addAttribute("cancelRenewal", InsuranceStatus.BU_BAO.getCode().equals(dhInsuranceRenewal.getStatusRenewal()));
//    				model.addAttribute("hasRenewal", InsuranceStatus.YI_BAO.getCode().equals(dhInsuranceRenewal.getStatusRenewal()));
//    			}
    			
    			DhInsuranceExt dhInsuranceExt = new DhInsuranceExt();
    			dhInsuranceExt.setBaseWhere();
    			dhInsuranceExt.setRows(1);
    			dhInsuranceExt.setRenewalId(insuranceRenewalId);
    			dhInsuranceExt.setBusiId(id);
    			List<DhInsurance> dhInsurances = dhInsuranceService.selectByExt(dhInsuranceExt);
    			if(CollectionUtils.isNotEmpty(dhInsurances)){
    				DhInsurance dhInsurance = dhInsurances.get(0);
    				model.addAttribute("dhInsurance", dhInsurance);
    				
    				//依附于保险的强险
    				DhInsuranceCompulsoryExt dhInsuranceCompulsoryExt = new DhInsuranceCompulsoryExt();
    				dhInsuranceCompulsoryExt.setBaseWhere();
    				dhInsuranceCompulsoryExt.setRows(1);
    				dhInsuranceCompulsoryExt.setInsuranceId(dhInsurance.getId());
    				dhInsuranceCompulsoryExt.setBusiId(id);
    				List<DhInsuranceCompulsory> dhInsuranceCompulsorys = dhInsuranceCompulsoryService.selectByExt(dhInsuranceCompulsoryExt);
    				if(CollectionUtils.isNotEmpty(dhInsuranceCompulsorys)){
    					DhInsuranceCompulsory dhInsuranceCompulsory = dhInsuranceCompulsorys.get(0);
    					model.addAttribute("dhInsuranceCompulsory", dhInsuranceCompulsory);
    				}
    			}
    		}
    		
    	}
    	viewScopeControllerHelper.preparedCarModel(model);
    	
    	return "system/loanAfter/insuranceRenewalDetail";
    }
    
    /**
     * @author xiongxiaotun <br/>
     * @Title: allocation  <br/>
     * @Description: 续保派单 <br/>
     * @param model
     * @return
     * @version V1.0  <br/>
     */
    @ApiOperation(value = "续保派单", response = Boolean.class, httpMethod="GET", tags={"续保派单"})
    @RequestMapping(value = "allocation", method = RequestMethod.GET)
    public String allocation(Model model) {
    	SysUser sysUser =(SysUser)getCurrentUser();
    	String orgId = sysUser.getOrgId();
    	List<SysUser> users = viewScopeControllerHelper.preparedAllocation(orgId, model);
    	
    	DhInsuranceRenewalExt dhInsuranceRenewalExt = new DhInsuranceRenewalExt();
    	dhInsuranceRenewalExt.withOutPage();
    	dhInsuranceRenewalExt.setBaseWhere();
        dhInsuranceRenewalExt.notAllocation();
        int countNotAllocation = dhInsuranceRenewalService.countByExt(dhInsuranceRenewalExt);
    	model.addAttribute("countNotAllocation", countNotAllocation);
    	
    	int userCounts = users.size();
    	int avg  = countNotAllocation / userCounts + (countNotAllocation % userCounts == 0 ? 0 : 1);
    	model.addAttribute("avg", avg);
    	
    	return "system/loanAfter/insuranceRenewalAllocation";
    }
	
}
